<template>
    <div class="app-container">

    <!-- 查询表单 -->
    <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="教师名称">
            <el-input v-model="searchTeacher.name" placeholder="教师名称"></el-input>
        </el-form-item>

        <el-form-item label="教师职称">
            <el-select v-model="searchTeacher.level" placeholder="教师职称">
            <el-option label="高级教师" :value="2"></el-option>
            <el-option label="普通教师" :value="1"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="入驻时间">
            <el-date-picker v-model="searchTeacher.create" placeholder="选择入驻时间"
                type="datetime" value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00"/>
        </el-form-item>

        <el-form-item label="截止时间">
            <el-date-picker v-model="searchTeacher.end" placeholder="选择截止时间"
                type="datetime" value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00"/>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="getList()">查询</el-button>
            <el-button type="default" @click="resetData()">清空</el-button>
        </el-form-item>
    </el-form>

    <!-- 教师列表 -->
    <el-table
        :data="list"
        stripe
        style="width: 100%">
        <el-table-column 
            label="序号"
            width="70"
            align="center">
            <template slot-scope="scope">
                {{(current-1) * size + scope.$index + 1}}
            </template>
        </el-table-column>
        
        <el-table-column label="名称" width="80" prop="name"/>

        <el-table-column label="职称" width="80">
            <template slot-scope="scope">
                {{scope.row.level===1?'普通教师':'高级教师'}}
            </template>
        </el-table-column>

        <el-table-column label="简介" prop="introduce" />

        <el-table-column label="语录" prop="quotation" />

        <el-table-column label="创建时间" prop="createTime" width="160"/>

        <el-table-column label="排序" prop="sort" width="60"/>

        <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
                <!-- 路由方式跳转到回显页面 -->
                <router-link :to="'/teacher/edit/'+scope.row.id">
                    <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
                </router-link>  
                <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
        
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @current-change="getList"
      :current-page="current"
      :page-size="size"
      layout="total, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
</template>

<script>
//1.引入js文件
import teacher from '@/api/teacher/teacher'

export default {
    //写核心代码
    // data:{
    // },
    data(){//定义变量和初始值
        return{
            current:1,
            size:4,
            total:0,
            searchTeacher:{
            },
            list:[]
        }
    },
    created(){//页面渲染前之前
        //调用方法
        this.getList()
    },
    methods:{//创建方法，调用js定义的方法
        //教师列表方法
        getList(current = 1){
            this.current = current
            teacher.getTeacherListPage(this.current,this.size,this.searchTeacher)
            .then(response =>{//请求成功
                //console.log(response)
                this.list = response.data.datas
                this.total = response.data.total
                console.log(this.list)
            })
            .catch(error =>{//请求失败
                console.log(error)
            })
        },
        resetData(){//清空
            //输入项清空
            this.searchTeacher = {}
            //查询所有
            this.getList()
        },
        removeDataById(id){//逻辑删除教师
            
            this.$confirm('此操作将删除该教师记录, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {//确定删除
                teacher.deleteTeacherById(id)
                .then(response =>{//删除成功
                    //提示删除成功
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    //刷新列表
                    this.getList()
                })
                .catch(error =>{//删除失败
                    this.$message({
                        type: 'error',
                        message: '删除失败!'
                    });
                })
            
            }).catch(() => {//取消删除
            this.$message({
                type: 'info',
                message: '已取消删除'
            });          
            });
        }
    }
}
</script>